package com.javasm.crm.service.aftersale.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasm.crm.domin.criteria.repositrory.OutOrderCriteria;
import com.javasm.crm.domin.entity.aftersale.Outorder;
import com.javasm.crm.domin.entity.aftersale.RefacOutApply;
import com.javasm.crm.domin.entity.base.BaseEntity;
import com.javasm.crm.mapper.aftersale.OutorderMapper;
import com.javasm.crm.service.aftersale.IOutorderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lirui
 * @since 2021-07-14
 */
@Service
@Transactional
public class OutorderServiceImpl extends ServiceImpl<OutorderMapper, Outorder> implements IOutorderService {


    public boolean generateNewOutOrder(RefacOutApply refacOutApply) {

        Outorder outorder = new Outorder();

        outorder.setApplyId(Integer.valueOf(refacOutApply.getId().toString()));
        outorder.setApplyCode(refacOutApply.getOdoCode());
        outorder.setCreateTime(LocalDateTime.now());
        outorder.setStatus(0);
        outorder.setCreateBy(refacOutApply.getCreateBy());
        outorder.setType(1);//订单类型：返厂出库订单

        boolean save = save(outorder);

        if (!save) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }

        String outorderCode = UUID.randomUUID().toString().substring(0, 5) + outorder.getId();

        UpdateWrapper<Outorder> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", outorder.getId()).set("outorder_Code", outorderCode);

        boolean update = update(updateWrapper);

        if (!update) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }

        return true;
    }

    @Override
    public Page<Outorder> getOutOrder(OutOrderCriteria outOrderCriteria) {

        Integer currentPage = outOrderCriteria.getCurrentPage();
        Integer pageSize = outOrderCriteria.getPageSize();

        QueryWrapper<Outorder> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<Outorder> lambda = queryWrapper.lambda();

        String outorderCode = outOrderCriteria.getOutorderCode();
        if (outorderCode != null && !"".equals(outorderCode)) {
            lambda.like(Outorder::getOutorderCode, outorderCode);
        }

        Integer type = outOrderCriteria.getType();
        if (type != null) {
            lambda.eq(Outorder::getType, type);
        }

        Integer status = outOrderCriteria.getStatus();
        if (status != null) {
            lambda.eq(Outorder::getStatus, status);
        }

        LocalDateTime startTime = outOrderCriteria.getStartTime();
        LocalDateTime endTime = outOrderCriteria.getEndTime();
        if (startTime != null && endTime != null) {
            lambda.between(BaseEntity::getCreateTime, startTime, endTime);
        }

        lambda.orderByDesc(BaseEntity::getCreateTime);

        Page<Outorder> page = new Page<>(currentPage, pageSize);
        page(page,queryWrapper);

        return page;
    }
}
